import {request, type RequestOptions} from '@/utils/request';
import {saveAs} from '@/utils/downloadFile'

// 获取当前网站路径
const currentPath = window.location.href;
// 获取eventId参数
const eventIdParam = currentPath.split('=')[1];
console.log(eventIdParam)

export async function updateEvent(
    params: API.EventUpEntity,
    options?: RequestOptions,) {
    return request<any>(`/api/event/updateEvent`, {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
        },
        data: params,
        ...(options || {successMsg: '更新事件'}),
    });
}

export async function getStatistics(
    options?: RequestOptions,) {
    return request<any>(`/api/event/getStatistics`, {
        method: 'GET',
        headers: {
            'Content-Type': 'application/json',
        },
        ...(options || {successMsg: '查询成功'}),
    });
}


export async function getEventUser(
    params: API.EventUpEntity,
    options?: RequestOptions,) {
    const {itemNameId: param0} = params;
    return request<any>(`/api/event/getAssignablePeople/${param0}`, {
        method: 'GET',
        headers: {
            'Content-Type': 'application/json',
        },
        ...(options || {successMsg: '查询成功'}),
    });
}

export async function deleteTbEventList(
    params: API.EventList,
    options?: RequestOptions,) {
    return request<any>(`/api/event/deleteTbEventById`, {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
        },
        data: params,
        ...(options || {successMsg: '删除成功'}),
    });
}

export async function addTbEventList(
    params: API.EventList,
    options?: RequestOptions,) {
    return request<any>(`/api/event/addTbEventList`, {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
        },
        data: params,
        ...(options || {successMsg: '添加成功'}),
    });
}

export async function getBindingEvents(
    body: API.EventDeptDTO,
    options?: RequestOptions,) {
    return request<any>(`/api/event/getBindingEvents`, {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
        },
        data: body,
        ...(options || {successMsg: '查询共'}),
    });
}

export async function updateBindingEvents(
    body: API.EventDeptDTO,
    options?: RequestOptions,) {
    return request<any>(`/api/event/updateBindingEvents`, {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
        },
        data: body,
        ...(options || {successMsg: '更新成功'}),
    });
}

export async function downloadTable() {
    let res = await request({
        url: "api/event/export",
        responseType: 'blob',
    })
    const {headers, data} = res;
    let filename = decodeURI(headers["content-disposition"].split("\'\'")[1]);
    let blob = new Blob([data], {type: headers["content-type"]});
    saveAs(blob, filename)
}

export async function importData(
    body: FormData,
    options?: RequestOptions) {
    return request<FormData>('api/event/import', {
        requestType: 'form',
        method: 'POST',
        data: body,
        ...(options || {}),
    });
}

export async function getEventHandlerList(
    params: API.EventList,
    options?: RequestOptions,
) {
    return request<{
        status?: string;
        info?: Record<string, any>;
        error?: Record<string, any>;
        details?: Record<string, any>;
    }>('api/event/getHandlingEvents', {
        method: 'POST',
        data: {
            page: '1',
            pageSize: '1000',
            ...params,
        },
        ...(options || {}),
    });
}

export async function getEventList(
    params: API.EventListDto,
    options?: RequestOptions,
) {
    if (eventIdParam) {
        params.eventid = Number(eventIdParam)
    }
    return request<{
        status?: string;
        info?: Record<string, any>;
        error?: Record<string, any>;
        details?: Record<string, any>;
    }>('api/event/getEventList', {
        method: 'POST',
        data: {
            page: '1',
            pageSize: '10',
            ...params,
        },
        ...(options || {}),
    });
}


export async function getMyEventList(
    params: API.EventListDto,
    options?: RequestOptions,
) {
    return request<{
        status?: string;
        info?: Record<string, any>;
        error?: Record<string, any>;
        details?: Record<string, any>;
    }>('api/event/getMyEventList', {
        method: 'POST',
        data: {
            page: '1',
            pageSize: '10',
            ...params,
        },
        ...(options || {}),
    });
}